/**
 * class name: islist,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://hcjs.googlecode.com/svn/trunk/core.js,
 * update: http://hcjs.googlecode.com/svn/trunk/islist.js
 * ▼▲ &#9660; &#9650; > &#9658; < &#9668;
 */
function islist(){
    var A = arguments,
        _ = this;
    _ = ext( _, ext( {
        id : 'islist'+ now(3),
        elem : me.body,
        cssname : 'islist',
        list : [
            {
            id : '',
            cls : '',
            css : '',
            elem : '',
            target : '',
            fn : function(){;}
            }
        ]
        }, A[0] ) );

    _.uitype = 'islist';

    _.elem = $(_.elem) || me.body;

    _.toggelem = function () {
        if ( _.active == this ) {
            return false;
        }
        if ( _.active ) {
            _.active.delClass('act');
        }
        _.active = this.addClass('act');

        var elem = $(attr(this,'elem')),
            target = $(attr(this,'target'));
        _.dp = $('datapool')||elm('div').appendTo(me.body).attr('id','datapool').attr('style', 'display:none;');
        if ( target.nodeName && elem.nodeName ) {
            while ( target.firstChild ) {
                _.dp.appendChild(target.firstChild);
            }
            target.appendChild(elem);
        }

    return _;
    };

}
islist.prototype = {
    add : function (m) {
        var _ = this;
        if ( !isArray(m) ) {
            var m = _.list;
            _.$.empty();
        } else {
            _.list = _.list.concat(m);
        }

        for ( var i=0, L= m.length; i<L; i++ ) {

            var li = elm('a').setClass('item').attr('href', '#').attr('elem',m[i].elem||'')
                    .attr('target',m[i].target||'').appendTo(_.$);

            if ( p( m[i], 'id' ) ) {
                li.id = !$( m[i].id ) ? m[i].id : '_item_'+ m[i].id + now(3);
            } else {
                li.id = '_item__'+ now(2)*Math.random();
            }

            if ( p( m[i], 'cls' ) ) {
                li.addClass(m[i].cls);
                if ( m[i].cls == 'act' ) {
                    _.toggelem.apply(li, []);
                }
            }

            if ( m[i].fn ) {
                if ( typeof m[i].fn=='function' ) {
                    li.onclick = function () {
                        m[i].fn.apply(this,[]);
                        return false;
                    };
                } else if ( typeof m[i].fn=='string' ) {
                    li.onclick = new Function( m[i].fn +';return false;' );
                } else {
                    li.onclick = function(){_.toggelem.apply(this,[]);return false;};
                }
            } else {
                li.onclick = function(){_.toggelem.apply(this, []);return false;};
            }

            if ( p(m[i], 'ico', 'src') ) {
                var ico = elm('img').appendTo(li).attr('src', m[i].ico.src)
                        .attr('alt', m[i].ico.alt || '' ).setClass('ico');
            }

            if ( m[i].label ) {
                var tx = elm('span').appendTo(li).setClass('txt').html(m[i].label);
            }
        }

    return _;
    },

    init : function () {
        var _ = this;
        if ( !_.$ ) {
            _.$ = elm( 'div' ).setClass('islist').appendTo( _.elem );
            _.$.id = _.id = !$(_.id) ? _.id : '_islist'+ _.id;
            p(_, 'cssname') && _.$.addClass(_.cssname);
        }

        _.add();

    return _;
    }
};